package com.example.demo.common;

import com.example.demo.domain.*;
import com.hiacent.vrs.gateway.tcsae157.Scene;
import com.hiacent.vrs.gateway.tcsae157.frame.BasicSafetyMessage;
import com.hiacent.vrs.gateway.tcsae157.frame.RoadSideInformation;
import com.hiacent.vrs.gateway.tcsae157.frame.RoadsideSafetyMessage;
import com.hiacent.vrs.gateway.tcsae157.frame.base.*;
import com.hiacent.vrs.gateway.tcsae157.frame.base.Warn;
import com.hiacent.vrs.gateway.tcsae157.frame.base.enums.IntersectionStatusObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <h3>demo</h3>
 * <p>抽取方法</p>
 *
 * @author : 于家勇
 * @date : 2022-08-13 15:26
 **/
public class Function {

    public RoadSideInformation getRtesEvent(String message, RtesRSI rtesRSI) {
        RoadSideInformation roadSideInformation = new RoadSideInformation();
        roadSideInformation.setMsgCount(1);
        roadSideInformation.setId(rtesRSI.getRsuId());

        Position3D position3D = new Position3D();
        position3D.setLon(rtesRSI.getRsu_lon());
        position3D.setLat(rtesRSI.getRsu_lat());
        roadSideInformation.setRefPos(position3D);

        RTEData rteData = new RTEData();
        rteData.setRteId(Integer.parseInt(rtesRSI.getRte_id()));
        rteData.setEventType(Integer.parseInt(rtesRSI.getEvent_type()));
        rteData.enumEventType();
        rteData.setEventSource(Integer.parseInt(rtesRSI.getEvent_source()));
        rteData.eunmEventSource();
        rteData.setTimestamp(Long.valueOf(rtesRSI.getTimestamp()));
        RSITimeDetails rsiTimeDetails = new RSITimeDetails();
        rsiTimeDetails.setStartTime(Long.valueOf(rtesRSI.getTimestamp()));
        rsiTimeDetails.setEndTime(Long.valueOf(rtesRSI.getEnd_time()));
        rteData.setTimeDetails(rsiTimeDetails);

        Position3D position3D1 = new Position3D();
        position3D1.setLon(rtesRSI.getEvent_lon());
        position3D1.setLat(rtesRSI.getEvent_lat());
        rteData.setPos(position3D1);

        List<RTEData> list = new ArrayList<>();
        list.add(rteData);

        roadSideInformation.setRtes(list);
        return roadSideInformation;
    }

    public RoadSideInformation getRtssEvent(String message, RtssRSI rtssRSI) {
        RoadSideInformation roadSideInformation = new RoadSideInformation();
        roadSideInformation.setMsgCount(1);
        roadSideInformation.setId(rtssRSI.getRsuId());

        Position3D position3D = new Position3D();
        position3D.setLon(rtssRSI.getSign_lon());
        position3D.setLat(rtssRSI.getSign_lat());
        roadSideInformation.setRefPos(position3D);

        RTSData rtsData = new RTSData();
        rtsData.setRtsId(Integer.parseInt(rtssRSI.getRts_id()));
        rtsData.setSignType(Integer.parseInt(rtssRSI.getSign_type()));
        rtsData.enumSignType();

        List<RTSData> list = new ArrayList<>();
        list.add(rtsData);

        roadSideInformation.setRtss(list);

        return roadSideInformation;
    }

    public RoadsideSafetyMessage getRsmEvent(String message, RSM rsm) {
        RoadsideSafetyMessage roadsideSafetyMessage = new RoadsideSafetyMessage();
        roadsideSafetyMessage.setId(rsm.getRsu_id());
        roadsideSafetyMessage.setMsgCount(1);

        Position3D position3D = new Position3D();
        position3D.setLon(rsm.getRsu_lon());
        position3D.setLat(rsm.getRsu_lat());
        roadsideSafetyMessage.setRefPos(position3D);

        ParticipantData participantData = new ParticipantData();
        participantData.setPtcId(Integer.parseInt(rsm.getPtcId()));
        participantData.setSource(Integer.parseInt(rsm.getSourceType()));
        participantData.enumSource();

        VehicleSize vehicleSize = new VehicleSize();
        vehicleSize.setLength(Double.parseDouble(rsm.getVeh_length()));
        vehicleSize.setWidth(Double.parseDouble(rsm.getVeh_width()));
        vehicleSize.setHeight(Double.parseDouble(rsm.getVeh_height()));
        participantData.setSize(vehicleSize);

        Double speed = Double.parseDouble(rsm.getSpeed()) * 3.6;
        participantData.setSpeed(speed.intValue());
        Double heading = Double.parseDouble(rsm.getHeading());
        participantData.setHeading(heading.intValue());

        List<ParticipantData> list = new ArrayList<>();
        list.add(participantData);
        roadsideSafetyMessage.setParticipants(list);

        return roadsideSafetyMessage;
    }

    public BasicSafetyMessage getBsmEvent(String message, BSM bsm) {
        BasicSafetyMessage basicSafetyMessage = new BasicSafetyMessage();
        basicSafetyMessage.setId(bsm.getId());
        basicSafetyMessage.setMsgCnt(1);
        basicSafetyMessage.setSecMark(200);

        Position3D position3D = new Position3D();
        position3D.setLon(bsm.getLon());
        position3D.setLat(bsm.getLat());
        basicSafetyMessage.setPos(position3D);

        basicSafetyMessage.setSpeed(Double.parseDouble(bsm.getSpeed()));
        basicSafetyMessage.setHeading(Double.parseDouble(bsm.getAngle()));

        VehicleSize vehicleSize = new VehicleSize();
        vehicleSize.setLength(Double.parseDouble(bsm.getLength()));
        vehicleSize.setWidth(Double.parseDouble(bsm.getWidth()));
        vehicleSize.setHeight(Double.parseDouble(bsm.getHeight()));
        basicSafetyMessage.setSize(vehicleSize);

        VehicleClassification vehicleClassification = new VehicleClassification();
        vehicleClassification.setClassification(11);
        vehicleClassification.enumClassification();

        basicSafetyMessage.setVehicleClass(vehicleClassification);

        VehicleSafetyExtensions vehicleSafetyExtensions = new VehicleSafetyExtensions();
        ExteriorLights exteriorLights = new ExteriorLights();
        if (bsm.getSignal().equals("1")){
            exteriorLights.setRightTurnSignalOn(true);
        }
        if (bsm.getSignal().equals("2")){
            exteriorLights.setLeftTurnSignalOn(true);
        }
        if (bsm.getSignal().equals("8")){
            exteriorLights.setParkingLightsOn(true);
        }
        if (bsm.getSignal().equals("9")){
            exteriorLights.setRightTurnSignalOn(true);
            exteriorLights.setParkingLightsOn(true);
        }
        if (bsm.getSignal().equals("10")){
            exteriorLights.setLeftTurnSignalOn(true);
            exteriorLights.setParkingLightsOn(true);
        }

        vehicleSafetyExtensions.setLights(exteriorLights);
        basicSafetyMessage.setSafetyExt(vehicleSafetyExtensions);

        return basicSafetyMessage;
    }

    public com.hiacent.vrs.gateway.tcsae157.frame.SPAT getSpatMsg(String message, SPAT spatMsg) {
        com.hiacent.vrs.gateway.tcsae157.frame.SPAT spat = new com.hiacent.vrs.gateway.tcsae157.frame.SPAT();
        spat.setId(spatMsg.getNode_reference_id());
        spat.setMsgCount(1);

        IntersectionState intersectionState = new IntersectionState();
        IntersectionStatusObject intersectionStatusObject = new IntersectionStatusObject();
        intersectionStatusObject.setFixedTimeOperation(true);

        NodeReferenceID nodeReferenceID = new NodeReferenceID();
        nodeReferenceID.setId(Integer.parseInt(spatMsg.getNode_reference_id()));
        nodeReferenceID.setLinkId(spatMsg.getLink());
        intersectionState.setIntersectionId(nodeReferenceID);

        List<PhaseState> phaseStates = new ArrayList<>();
        PhaseState phaseState = new PhaseState();
        phaseState.setLight(Integer.parseInt(spatMsg.getLight_state_code()));
        phaseState.enumLight();
        TimeChangeDetails timing = new TimeChangeDetails();
        TimeCountingDown counting = new TimeCountingDown();
        Double phaseDuration = Double.parseDouble(spatMsg.getPhase_duration());
        counting.setNextDuration(phaseDuration.intValue());
        counting.setMinEndTime(Integer.parseInt(spatMsg.getMin_end_time()));
        counting.setMaxEndTime(Integer.parseInt(spatMsg.getMax_end_time()));
        timing.setCounting(counting);
        phaseState.setTiming(timing);
        phaseStates.add(phaseState);

        List<Phase> list = new ArrayList<>();
        Phase phase = new Phase();
        phase.setId(Integer.parseInt(spatMsg.getPhase_id()));
        phase.setPhaseStates(phaseStates);
        list.add(phase);
        intersectionState.setPhases(list);

        List<IntersectionState> intersections = new ArrayList<>();
        intersections.add(intersectionState);
        spat.setIntersections(intersections);

        return spat;

    }

    public Scene getWarnMsg(String message, Sceneing sceneing) {
        Warn warn = new Warn();
        warn.setVehicleId(sceneing.getVehicle_id());
        warn.setLicensePlateNumber(sceneing.getLicense_plate_number());
        warn.setType(Integer.valueOf(sceneing.getWarn_type()));
        warn.enumType();

        Message msg = new Message();
        msg.setHead(Double.parseDouble(sceneing.getHead()));
        msg.setAdviceSpeed(Integer.valueOf(sceneing.getAdvice_speed()));
        msg.setMinSpeed(Integer.valueOf(sceneing.getMin_speed()));
        msg.setDist(Double.parseDouble(sceneing.getDist()));
        msg.setDistV(Double.parseDouble(sceneing.getDistV()));
        msg.setDistH(Double.parseDouble(sceneing.getDistH()));
        Double speed = Double.parseDouble(sceneing.getSelf_speed());
        msg.setSelfSpeed(speed.intValue());
        msg.setMaxSpeed(Integer.parseInt(sceneing.getMax_speed()));
        Position3D pos = new Position3D();
        BigDecimal lon = new BigDecimal(sceneing.getLon());
        BigDecimal lat = new BigDecimal(sceneing.getLat());
        pos.setLon(lon);
        pos.setLat(lat);
        msg.setPos(pos);

        if (sceneing.getColor() != null) {
            msg.setColor(Integer.parseInt(sceneing.getColor()));
            msg.setCountDownSec(Integer.parseInt(sceneing.getCount_down_sec()));
            warn.setMsg(msg);
        }else {
            warn.setMsg(msg);
        }

        Scene scene = new Scene();
        scene.setWarn(warn);
        scene.setId(sceneing.getUid());

        return scene;
    }

}
